package com.rwy.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;

public class StringUtil  {
	/* 随机字符串生成的方式（由字母和数字组成） */
	public static final int RANDOM_TYPE_NORMAL = 1;
	/* 随机字符串生成的方式（全部由数字组成） */
	public static final int RANDOM_TYPE_ALNUM = 2;
	/* 随机字符串生成的方式（全部由字母组成） */
	public static final int RANDOM_TYPE_ALPHA = 3;

	/**
     * 全角字符串转换半角字符串
     * 
     * @param fullWidthStr
     *            非空的全角字符串
     * @return 半角字符串
     */
    public static String fullWidth2halfWidth(String fullWidthStr) {
        if (null == fullWidthStr || fullWidthStr.length() <= 0) {
            return "";
        }
        char[] charArray = fullWidthStr.toCharArray();
        //对全角字符转换的char数组遍历
        for (int i = 0; i < charArray.length; ++i) {
            int charIntValue = (int) charArray[i];
            //如果符合转换关系,将对应下标之间减掉偏移量65248;如果是空格的话,直接做转换
            if (charIntValue >= 65281 && charIntValue <= 65374) {
                charArray[i] = (char) (charIntValue - 65248);
            } else if (charIntValue == 12288) {
                charArray[i] = (char) 32;
            }
        }
        return new String(charArray);
    }
    
    public static boolean handle(int n,int length) {
		int[] list = new int[length];
		for (int i = 0; i < length; i++) {
			list[i] = n % 10;
			n = n / 10;
		}
		for (int i = 0; i < length; i++) {
			for (int j = i + 1; j < length; j++) {
				if (list[i] == list[j])
					return false;
			}
		}
		return true;
	}
    
	public static boolean isEmpty(String str) {
		if (str == null) {
			return true;
		} else {
			if (str.trim().equals("") || "null".equalsIgnoreCase(str)) {
				return true;
			}
			
		}
		return false;
	}

	public static boolean isNotEmpty(String str) {
		if (!isEmpty(str)) {
			return true;
		}
		return false;
	}
	
	public static boolean isNotEmptyORNull(String str) {
        return null != str && !"".equals(str) && !"null".equals(str);
    }
	
	public static String numberFormat(Double number, String format) {
		if (number == null || format == null || "".equals(format)) {
			return "";
		}
		
		try {
			DecimalFormat df = new DecimalFormat(format);
			
			return df.format(number);
		} catch (Exception e) {
			return "";
		}
	}
	
	public static String hideString(String input, int startPos, int endPos) {
		if (isEmpty(input)) {
			return input;
		}
		
		int len = input.length();
		
		if (startPos < 0 || endPos > (len - 1)) {
			return input;
		}
		
		char[] chars = input.toCharArray();
		
		StringBuffer sb = new StringBuffer();
		
		for (int i = 0; i < chars.length; i++) {
			if (i < startPos || i > endPos) {
				sb.append(chars[i]);
			}
			else {
				sb.append('*');
			}
		}
		
		return sb.toString();
	}
	
	
	public static String keepSecretString(String firstName, String lastName ) {
		
		String keepSecretString   = firstName +" "+ lastName;
		StringBuffer sb = new StringBuffer();
		if(keepSecretString.length() < keepSecretString.getBytes().length){
			keepSecretString = keepSecretString.replace(" ", "");
			//中文判断
			char[] chars = keepSecretString.toCharArray();
			System.out.println(chars.length);
			for (int i = 0; i < chars.length; i++) {
				if (i < 1 ) {
					sb.append(chars[i]);
				}
				else {
					sb.append('*');
				}
			}
		}else{
			
			if (StringUtil.isNotEmpty(firstName) && StringUtil.isNotEmpty(lastName) ) {
				char[] firstNameChars = firstName.toCharArray();
				char[] lastNameChars = lastName.toCharArray();
				for (int i = 0; i < firstNameChars.length; i++) {
					if (i < 1 || i > firstNameChars.length ) {
						sb.append(firstNameChars[i]);
					}
					else {
						sb.append('*');
					}
				}
				sb.append(" ");
				for (int i = 0; i < lastNameChars.length; i++) {
					if (i < 0 || i > lastNameChars.length-2 ) {
						sb.append(lastNameChars[i]);
					}
					else {
						sb.append('*');
					}
				}
			}
			else if(StringUtil.isEmpty(firstName)||StringUtil.isEmpty(lastName)) {
				keepSecretString = keepSecretString.replace(" ", "");
				char[] chars = keepSecretString.toCharArray();
				for (int i = 0; i < chars.length; i++) {
					if (i < 1 || i > chars.length-2 ) {
						sb.append(chars[i]);
					}
					else {
						sb.append('*');
					}
				}
			}
		}
		return sb.toString();
		 
	}
	
	/**
	 * 格式化输出电话号码和传真号码
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 * 
	 * @param str 待处理的字符串
	 * @return
	 */
	public static String getFriendlyPhoneNumber(String str) {
		if (isEmpty(str)) {
			return "";
		}
		
		Pattern pattern;
		Matcher matcher;
		
		String countryCode = "";
		String areaCode = "";
		String number = "";
		
		pattern = Pattern.compile("^\\([+]*([0-9]*)\\)([0-9]+)[-]+([0-9]+)$");
		matcher = pattern.matcher(str);
		while (matcher.find()) {
			countryCode = matcher.group(1);
			areaCode = matcher.group(2);
			number = matcher.group(3);
			return "(+" + countryCode + ")" + areaCode + "-" + number;
		}
		
		matcher.reset();
		pattern = Pattern.compile("^([0-9]+)[-]+([0-9]+)$");
		matcher = pattern.matcher(str);
		while (matcher.find()) {
			areaCode = matcher.group(1);
			number = matcher.group(2);
			return areaCode + "-" + number;
		}
		
		matcher.reset();
		pattern = Pattern.compile("^([0-9]+)-([0-9]+)-([0-9]+)$");
		matcher = pattern.matcher(str);
		while (matcher.find()) {
			countryCode = matcher.group(1);
			areaCode = matcher.group(2);
			number = matcher.group(3);
			return "(+" + countryCode + ")" + areaCode + "-" + number;
		}
		
		matcher.reset();
		pattern = Pattern.compile("^([0-9]+)-([0-9]+)$");
		matcher = pattern.matcher(str);
		while (matcher.find()) {
			areaCode = matcher.group(1);
			number = matcher.group(2);
			return areaCode + "-" + number;
		}
		
		return str;
	}
	
	/**
	 * 格式化输出手机号码
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 * 
	 * @param str 待处理的字符串
	 * @return
	 */
	public static String getFriendlyMobileNumber(String str) {
		if (isEmpty(str)) {
			return "";
		}
		
		Pattern pattern;
		Matcher matcher;
		
		String countryCode = "";
		String number = "";
		
		pattern = Pattern.compile("^\\([+]*([0-9]*)\\)([0-9]+)$");
		matcher = pattern.matcher(str);
		while (matcher.find()) {
			countryCode = matcher.group(1);
			number = matcher.group(2);
			return "(+" + countryCode + ")" + number;
		}
		
		matcher.reset();
		pattern = Pattern.compile("^([0-9]+)-([0-9]+)$");
		matcher = pattern.matcher(str);
		while (matcher.find()) {
			countryCode = matcher.group(1);
			number = matcher.group(2);
			return "(+" + countryCode + ")" + number;
		}
		
		return str;
	}
	
	public static String getImageAbslouteUrl(String url, String mediaPath) {
		if (!isNotEmpty(url) || !isNotEmpty(mediaPath)) {
			return "";
		}
		if (url.startsWith("/")) {
			return mediaPath + url;
		}
		return mediaPath + "/" + url;
	}
	
	public static String replaceSign(String str) {
		if (isNotEmpty(str) && str.indexOf(",") != -1) {
			str = str.replaceAll(",", "\\|");
		}
		return str;
	}
	
	public static String replaceSign(String str, String from, String to) {
		if (isNotEmpty(str) && str.indexOf(from) != -1) {
			str = str.replaceAll(from, to);
		}
		return str;
	}
	/**
	 * 生成随机字符串
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 * 
	 * @param len 随机字符串的长度
	 * @return
	 */
	public static String getRandomString(int len) {
		return getRandomString(len, RANDOM_TYPE_NORMAL);
	}
	
	/**
	 * 按指定的方式生成随机字符串
	 * 
	 * @author Simon.Ye <samlinye@163.com>
	 * 
	 * @param len 随机字符串的长度
	 * @param randomType 生成随机字符串的方式 
	 * @return
	 */
	public static String getRandomString(int len, int randomType) {
		if (len < 1) {
			return "";
		}
		
		String str;
		
		if (randomType == RANDOM_TYPE_ALNUM) {
			str = "3456789987654334567899876543";
		}
		else if (randomType == RANDOM_TYPE_ALPHA) {
			str = "abcdefghjkmnpqrstuvwxyYXWVUTSRQPNMKJHGFEDCBAyxwvutsrqpnmkjhgfedcbaABCDEFGHJKMNPQRSTUVWXY";
		}
		else {
			str = "yxwvutsrqpnmkjhgfedcba3456789ABCDEFGHJKMNPQRSTUVWXY99876543ABCDEFGHJKMNPQRSTUVWXYabcdefghjkmnpqrstuvwxy";
		}
		
		Random rnd = new Random();
		
		StringBuilder sb = new StringBuilder();
		
		for (int i = 0; i < len; i++) {
			int index = rnd.nextInt(str.length());
			sb.append(str.charAt(index));
		}
		
		return sb.toString();
	}
	
	public static String getUUID() {
		return UUID.randomUUID().toString();
	}
	
	public static boolean isBlank(final String str) {
		
		int length;
		if (str == null || (length = str.length()) == 0) {
			return true;
		}
		
		for (int i = 0; i < length; i++) {
			if (!Character.isWhitespace(str.charAt(i))) {
				return false;
			}
		}
		return true;
		
	}
	
	public static String replace(String inString, String oldPattern, String newPattern) {
		if (!hasLength(inString) || !hasLength(oldPattern) || newPattern == null) {
			return inString;
		}
		StringBuilder sb = new StringBuilder();
		int pos = 0; // our position in the old string
		int index = inString.indexOf(oldPattern);
		// the index of an occurrence we've found, or -1
		int patLen = oldPattern.length();
		
		while (index >= 0) {
			sb.append(inString.substring(pos, index));
			sb.append(newPattern);
			pos = index + patLen;
			index = inString.indexOf(oldPattern, pos);
		}
		
		sb.append(inString.substring(pos));
		// remember to append any characters to the right of a match
		return sb.toString();
	}
	
	public static boolean hasLength(CharSequence str) {
		return (str != null && str.length() > 0);
	}
	
	public static boolean hasLength(String str) {
		return hasLength((CharSequence) str);
	}
	
	public static byte[] getUtf8Bytes(String str){
		if(hasLength(str)){
			try {
				return str.getBytes("UTF-8");
			} catch (UnsupportedEncodingException e) {;}
		}
		return null;
	}
	
	public static String getStringFromUtf8Bytes(byte[] tmpArray) {
		if(tmpArray != null && tmpArray.length > 0){
			try {
				return new String(tmpArray,"UTF-8");
			} catch (UnsupportedEncodingException e) {;}
		}
		return null;
	}
	
	public static boolean equals(String str1, String str2) {
		if(str1==null && str2 == null){
			return true;
		}
		
		if(str1 == null || str2 == null){
			return false;
		}
		
		return str1.equals(str2);
	}
	
	public static String gzip(String primStr) {
    	if (primStr == null || primStr.length() == 0) {
    		return primStr;
    	}

    	ByteArrayOutputStream out = new ByteArrayOutputStream();

    	GZIPOutputStream gzip=null;
    	try {
    		gzip = new GZIPOutputStream(out);
    		gzip.write(primStr.getBytes());
    	} catch (IOException e) {
//    		e.printStackTrace();
    		;
    	}finally{
    		if(gzip!=null){
    			try {gzip.close();} catch (IOException e) {
//    				e.printStackTrace();
    				;
    			}
    		}
    	}
//    	return new sun.misc.BASE64Encoder().encode(out.toByteArray());
    	return Base64.encodeBase64String(out.toByteArray());
    }
    
    public static String gunzip(String compressedStr){
    	if(compressedStr==null){
    		return null;
    	}
    	
    	 ByteArrayOutputStream out= new ByteArrayOutputStream();
    	 ByteArrayInputStream in=null;
	   	 GZIPInputStream ginzip=null;
	   	 byte[] compressed=null;
	   	 String decompressed = null;
	   	 try {
	//   	 compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
	   	     compressed	= Base64.decodeBase64(compressedStr);
	
	   	     in=new ByteArrayInputStream(compressed);
	   	     ginzip=new GZIPInputStream(in);
	
	   	     byte[] buffer = new byte[1024];
	   	     int offset = -1;
	   	     while ((offset = ginzip.read(buffer)) != -1) {
	   	    	 out.write(buffer, 0, offset);
	   	     }
	   	     decompressed=out.toString();
	   	 } 
	   	 catch (IOException e) {
//	   		 e.printStackTrace();
	   		 ;
	   	 } finally {
	   		 if (ginzip != null) {
	   			 try {ginzip.close();} catch (IOException e) {;}
	   		 }
	   		 if (in != null) {
	   			 try {
	   				 in.close();
	   			 } catch (IOException e) {;}
	   		 }
	   		 if (out != null) {
	   			 try {
	   				 out.close();
	   			 } catch (IOException e) {;}
	   		 }
	   	}

   	 return decompressed;
    }
    
    public static String getEmailDomain(String email) {
    	return StringUtil.isEmpty(email) ? null : email.substring(email.indexOf("@")+1);
    }
    
	public static int getPasswordLevel(String value) {
		String level1 = "\\d+|[a-zA-Z]+|[\\w+]{6,8}";
		String level3 = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,32})";
		if (Pattern.matches(level3, value)) {
			return 3;
		}
		if (Pattern.matches(level1, value)) {
			return 1;
		}
		return 2;
	}
	
	public static String getString(Object obj, String str) {
		return obj==null?str:String.valueOf(obj);
	}
	
	public static String getString(Object obj) {
		return getString(obj, "");
	}
	
	public static Integer getInteger(Object obj, String str) {
		return Integer.valueOf(getString(obj, str));
	}
	
	public static BigDecimal getBigDecimal(Object obj) {
		return new BigDecimal(getString(obj, "0"));
	}
	
	/** 
     * 对字符串处理:将指定位置到指定位置的字符以星号代替 
     *  
     * @param content 
     *            传入的字符串 
     * @param begin 
     *            开始位置 
     * @param end 
     *            结束位置 
     * @return 
     */  
	public static String getStarString(String content, int begin, int end) {  
  
        if (begin > content.length() || begin < 0) {  
            return content;  
        }  
        if (end > content.length() || end < 0) {  
            return content;  
        }  
        if (begin >= end) {  
            return content;  
        }  
        String starStr = "";  
        for (int i = begin; i < end; i++) {  
            starStr = starStr + "*";  
        }  
        return content.substring(0, begin) + starStr + content.substring(end, content.length());  
  
    }  
      
    /** 
     * 对字符加星号处理：除前面几位和后面几位外，其他的字符以星号代替 
     *  
     * @param content 
     *            传入的字符串 
     * @param frontNum 
     *            保留前面字符的位数 
     * @param endNum 
     *            保留后面字符的位数 
     * @return 带星号的字符串 
     */  
  
    public static String getStarString2(String content, int frontNum, int endNum) {  
  
        if (frontNum >= content.length() || frontNum < 0) {  
            return content;  
        }  
        if (endNum >= content.length() || endNum < 0) {  
            return content;  
        }  
        if (frontNum + endNum >= content.length()) {  
            return content;  
        }  
        String starStr = "";  
        for (int i = 0; i < (content.length() - frontNum - endNum); i++) {  
            starStr = starStr + "*";  
        }  
        return content.substring(0, frontNum) + starStr  
                + content.substring(content.length() - endNum, content.length());  
  
    }
    
    public static boolean isMobileNO(String mobiles){  
		  
		Pattern p = Pattern.compile("^((13[0-9])|(17[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");  
		  
		Matcher m = p.matcher(mobiles);  
		  
		//System.out.println(m.matches()+"---");  
		  
		return m.matches();  
		  
	}
	
	public static boolean isEmail(String email) {
		if(!email.matches("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$")){
		   return false;
		}else{
		   return true;
		}
	}
	
	
	public static String getAuthCode() {
		int n = 0;
		n = (int) (Math.random() * 1000000);
		while (n < 100000 || !handle(n,6)) {
			n = (int) (Math.random() * 1000000);
		}
		return String.valueOf(n);
	}
    
}

